<template>
	<view class="mainpadding">
		<view class="xiahuaxian flexbetween">
			<input class="" type="number" placeholder="请输入原手机号验证码" v-model="y_captcha">
			<view class="xiaohong strongtext" style="min-width: 200rpx;" @click="sendyzm" v-show="codetext == 1">发送验证码</view>
			<view class="xiaohong strongtext" style="min-width: 200rpx;" v-show="codetext == 2">{{second}}s后重新获取</view>
			<view class="xiaohong strongtext" style="min-width: 200rpx;" @click="sendyzm" v-show="codetext == 3">重新发送</view>
		</view>
		<view class="xiahuaxian margin_top">
			<input class="" type="number" placeholder="请输入新绑定手机号" v-model="mobile">
		</view>
		<view class="xiahuaxian margin_top flexbetween">
			<input class="" type="number" placeholder="请输入新绑定手机号验证码" v-model="captcha">
			<view class="xiaohong strongtext" style="min-width: 200rpx;" @click="sendyzmt" v-show="codetextt == 1">发送验证码</view>
			<view class="xiaohong strongtext" style="min-width: 200rpx;" v-show="codetextt == 2">{{secondt}}s后重新获取</view>
			<view class="xiaohong strongtext" style="min-width: 200rpx;" @click="sendyzmt" v-show="codetextt == 3">重新发送</view>
		</view>
		<view class="gudingdb ffffff mainpadding">
			<view class="bighbtn" @click="submit">提交</view>
		</view>
	</view>
</template>

<script>
	const httpRequest = require("@/common/httpRequest.js")
	export default {
		data() {
			return {
				y_mobile:"",
				mobile:"",
				captcha:"",
				y_captcha:"",
				codetext: 1,
				second: 59,
				codetextt: 1,
				secondt: 59,
			}
		},
		onShow() {
			this.init()
		},
		methods: {
			submit(){
				if(this.y_captcha==""){
					httpRequest.toast("请输入原手机号验证码")
					return false
				}
				if(this.mobile==""){
					httpRequest.toast("请输入新绑定手机号")
					return false
				}
				if(this.captcha==""){
					httpRequest.toast("请输入新绑定手机号验证码")
					return false
				}
				httpRequest.request('/api/user/changemobile', 'GET', {
					mobile:this.mobile,
					captcha:this.captcha,
					y_captcha:this.y_captcha,
				}).then(res => {
					if(res.code==1){
						uni.removeStorageSync('token');
						uni.removeStorageSync('user_id');
						httpRequest.toast("修改手机号成功，已自动为您退出登录")
						setTimeout(() => {
							uni.navigateBack({
								delta:2
							})
						}, 1000)
					}else{
						httpRequest.toast(res.msg)
					}
				})
			},
			init() {
				httpRequest.request('/api/user/index', 'GET', {}).then(res => {
					this.y_mobile = res.data.mobile
				})
			},
			sendyzm() {//原手机号发送验证码
				let _this = this;
				_this.codetext = 2;
				_this.second = 59;
				let timer = setInterval(function() {
					if (_this.second > 1) {
						_this.second--;
					} else {
						clearInterval(timer);
						_this.codetext = 3;
					}
				}, 1000)
				httpRequest.request('/api/sms/send', 'POST', {
					mobile: this.y_mobile,
					event: 'getchangemobile',
				}, false, true, false).then(res => {
					if (res.code == 1) {
						httpRequest.toast('发送成功')
					}
				})
			},
			sendyzmt() {//新手机号发送验证码
				let _this = this;
				if (_this.mobile == '') {
					httpRequest.toast("请输入手机号码!")
					return;
				}
				_this.codetextt = 2;
				_this.secondt = 59;
				let timer = setInterval(function() {
					if (_this.secondt > 1) {
						_this.secondt--;
					} else {
						clearInterval(timer);
						_this.codetextt = 3;
					}
				}, 1000)
				httpRequest.request('/api/sms/send', 'POST', {
					mobile: this.mobile,
					event: 'changemobile',
				}, false, true, false).then(res => {
					if (res.code == 1) {
						httpRequest.toast('发送成功')
					}
				})
			},
		}
	}
</script>

<style lang="scss" scoped>

</style>
